package com.bawei.module_moto_circle.ui.post.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import com.bawei.lib_common.widget.nine_pic_grid.Image

import com.bawei.module_moto_circle.R
import com.bawei.module_moto_circle.databinding.LayoutPostItemBinding

import com.bawei.module_moto_circle.model.PostInfo

/**
 *作者：lxy
 *功能:
 *时间：2025/6/1
 */
class PostPagingAdapter(
    private val onItemClick: ((PostInfo, Int) -> Unit)? = null,
    private val onUserClick: ((PostInfo) -> Unit)? = null,
    private val onImageClick: ((Int, Int, List<Image>) -> Unit)? = null,
    private val onShareClick: ((PostInfo) -> Unit)? = null,
    private val onCommentsClick: ((PostInfo) -> Unit)? = null,
    private val onLikeClick: ((PostInfo, Int) -> Unit)? = null,
    private val onMoreActionClick: ((PostInfo, Int) -> Unit)? = null,
    private val onLikeViewClick: ((PostInfo, Int, View) -> Unit)? = null,
    private val onCollectionViewClick: ((PostInfo, Int, View) -> Unit)? = null,
    private val onFollowViewClick: ((PostInfo, Int, View) -> Unit)? = null,
    private val showAuthorLabel: Boolean = true,
    // 是否显示关系
    private var showRelationState: Boolean = true
) : PagingDataAdapter<PostInfo, PostViewHolder>(POST_DIFF) {

    private val mData = mutableListOf<PostInfo>()

    companion object {
        private val POST_DIFF = object : DiffUtil.ItemCallback<PostInfo>() {
            override fun areItemsTheSame(oldItem: PostInfo, newItem: PostInfo): Boolean {
                return oldItem.posts_id == newItem.posts_id
            }

            override fun areContentsTheSame(oldItem: PostInfo, newItem: PostInfo): Boolean {
                return oldItem == newItem
            }
        }
    }

    enum class PostType(val value: Int) {
        Pic(1), Video(2)
    }

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        val binding = DataBindingUtil.getBinding<LayoutPostItemBinding>(holder.itemView)
        val theItem = getItem(position)
        if (theItem != null) {
            theItem.needShowFollowState = showRelationState
            //设置图片
            holder.setMaxLines(2)
            holder.initView(theItem, position)
            binding?.post = theItem
            binding?.holder = holder
            binding?.lFollowState?.isSelected = theItem.hasFollowed()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {

        val binding: LayoutPostItemBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.layout_post_item,
            parent,
            false
        )

        val postViewHolder = PostViewHolder(
            binding.root,
            binding,
            onUserClick,
            onItemClick,
            onImageClick,
            onShareClick,
            onCommentsClick,
            onLikeClick,
            onMoreActionClick,
            onLikeViewClick,
            onCollectionViewClick,
            onFollowViewClick
        )
        binding.holder = postViewHolder

        binding.showAuthorLabel = showAuthorLabel

        return postViewHolder
    }


    //获取数据源
    fun getPostItem(position: Int): PostInfo? {
        if (position < itemCount)
            return getItem(position)
        return null
    }


    //修改数据源
    fun updatePostItem(position: Int, postInfo: PostInfo) {
        run {
            repeat(itemCount) {
                val source = getPostItem(it)
                if (source?.posts_id == postInfo.posts_id) {
                    source.apply {
                        if_like = postInfo.if_like
                        like_number = postInfo.like_number
                        comment_number = postInfo.comment_number
                        notifyItemChanged(position, "update")
                        return@run
                    }
                }
            }
        }
    }

    //添加数据源
    fun setPostItem(data: List<PostInfo>) {
        mData.clear()
        mData.addAll(data)
        val size = mData.size
        notifyItemRangeInserted(size - 1, data.size)
    }

}